<template>
  <div class="product">
    <!-- 搜索框 -->
    <div class="search">
      <template>
        <el-select
          v-model="searchValue"
          filterable
          clearable
          remote
          @change="searchTextChange"
          reserve-keyword
          @clear="clear"
          placeholder="请输入姓名,如果不更新,点击右侧搜索按钮"
          :remote-method="remoteMethod"
          :loading="loading"
        >
          <el-option
            v-for="item in searchList"
            :key="item.sId"
            :label="item.name"
            :value="item.name"
          >
          </el-option>
        </el-select>
      </template>

      <!-- 搜索按钮 -->
      <el-button
        type="primary"
        icon="el-icon-search"
        @click="onSearchButtonClick"
        plain
      ></el-button>

      <!-- 添加按钮 -->
      <el-button type="primary" @click="addStu" :disabled="disabled" v-has="'add'"
        >添加项目</el-button
      >
    </div>

    <!-- 班级选择 -->
    <div class="select-class">
      <!-- 搜索框 -->
      <el-form>
        <el-form-item label="选择班级:">
          <el-select
            v-model="classes"
            filterable
            @visible-change="classVisible"
            @change="classChange"
            placeholder="请选择"
          >
            <el-option
              v-loading="selectLoading"
              label="全部"
              value="all"
            ></el-option>
            <el-option
              v-for="(item, index) in classList"
              :key="index"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>

    <!-- 导出excel -->
    <div class="exportExcel">
      <el-button icon="iconfont icon-excel" @click="exportExcel"
        >导出excel数据</el-button
      >
    </div>

    <!-- 表格 -->
    <div class="stu-table">
      <el-table :data="stuData" v-loading="loading" border style="width: 100%">
        <el-table-column
          prop="headimgurl"
          label="头像"
          width="100"
          align="center"
        >
          <!-- 插槽作用域 -->
          <template v-slot="scope">
            <el-avatar
              shape="square"
              :size="50"
              fit="fill"
              :src="scope.row.headimgurl"
            ></el-avatar>
          </template>
        </el-table-column>
        <el-table-column prop="name" width="100" label="姓名" align="center">
        </el-table-column>
        <el-table-column prop="degree" label="学历" align="center">
        </el-table-column>
        <el-table-column prop="class" width="100" label="班级" align="center">
        </el-table-column>
        <el-table-column prop="productUrl" label="项目" align="center">
        </el-table-column>
        <el-table-column prop="cTime" label="创建时间" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template v-slot="{ row }">
            <el-button
              type="primary"
              class="btn"
              v-has="'read'"
              icon="el-icon-view"
              @click="$router.push({ path: '/studentManager/studentProfile' })"
              >查看</el-button
            >
            <el-button
              type="primary"
              icon="el-icon-edit"
              class="btn"
              v-has="'edit'"
              @click="editStu(row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              class="btn"
              v-has="'delete'"
              @click="deleteStu(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页器 -->
    <div class="pagination">
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page.sync="currentPage"
        @current-change="changePage"
        :page-size="stuPageSize"
        :total="stuTotal"
      >
      </el-pagination>
    </div>

    <!-- dialog弹出框 -->
    <qf-dialog></qf-dialog>
  </div>
</template>
<script>
import dialog from "../../../../components/Dialog";
import * as api from "../../../../api";
import { mapState, mapActions, mapMutations } from "vuex";
import qee from "qf-export-excel"; //前端导出excel的方法
export default {
  components: {
    "qf-dialog": dialog,
  },
  computed: {
    ...mapState(["stuData", "loading", "stuTotal", "stuPageSize"]),
  },
  data() {
    return {
      dataPage: "", //页码
      dataCount: "", //每页展示的数量
      total: 0, //总共的数据条数
      classList: [], //班级列表
      classOptions: [],
      disabled: false,
      classes: "", //select绑定的班级
      selectLoading: true,
      searchList: [],
      searchValue: "",
      currentPage: 1,
    };
  },
  created() {
    this.GET_STULIST(); //获取学员数据列表
  },
  methods: {
    ...mapActions(["GET_STULIST"]),
    ...mapMutations([
      "SHOW_DIALOG",
      "CHANGE_DIALOG_TITLE",
      "SET_CURRENT_ROW",
      "SET_STU_DATA",
    ]),

    addStu() {
      this.CHANGE_DIALOG_TITLE("增加学员信息"), this.SHOW_DIALOG();
    },

    editStu(row) {
      console.log(row);
      this.CHANGE_DIALOG_TITLE("编辑学员信息"), //修改dialog标题
        this.SHOW_DIALOG();
      //将当前这行数据回显到dialog弹出框,通过vuex修改currentRow的值
      this.SET_CURRENT_ROW(row);
    },

    deleteStu(row) {
      // console.log(row);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          api.delStuApi(row.sId).then((res) => {
            if (res.data.state) {
              //删除成功
              //更新列表
              this.GET_STULIST();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            } else {
              //删除失败  这条数据已经被别人删除了
              this.$message.error("删除失败，数据不存在");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "warning",
            message: "已取消删除",
          });
        });
    },

    classVisible(isVisible) {
      this.selectLoading = true; //展开选择器的时候loading变化true
      //展开的时候获取数据
      //调用获取列表的方法
      // console.log(isVisible);
      if (isVisible) {
        //下拉框出现了
        //发送请求，获取班级列表
        api.getClassesApi().then((res) => {
          // console.log(res);
          this.classList = res.data.data;
          this.selectLoading = false;
        });
      }
    },

    classChange(query) {
      console.log(query);
      if (query === "all") {
        query = ""; //不传值后台默认显示所有数据
      }
      //调用GET_STULIST,传入班级
      this.GET_STULIST({ classes: query });
    },

    changePage(page) {
      if (this.searchValue) {
        //判断搜索框是否有值
        api.searchStuApi(null, this.searchValue, null).then((res) => {
          this.SET_STU_DATA(res.data);
        });
      } else {
        // console.log(page);
        //页码变更，传给后台，重新获取数据
        this.GET_STULIST({ page });
      }
    },

    clear() {
      console.log(currentPage);
      this.currentPage = 1;
    },

    exportExcel() {
      let titleList = [
        {
          title: "头像地址",
          key: "headimgurl",
        },
        {
          title: "姓名",
          key: "name",
        },
        {
          title: "学历",
          key: "degree",
        },
        {
          title: "班级",
          key: "class",
        },
        {
          title: "项目地址",
          key: "productUrl",
        },
        {
          title: "创建时间",
          key: "cTime",
        }
      ];
      qee(titleList,this.stuData,"H2104班级信息")
    },

    remoteMethod(query) {
      // console.log(query);
      this.searchValue = query;
      api.searchStuApi(null, query, null).then((res) => {
        // console.log(res.data.data);
        //数组去重
        let norepeat = [];
        // let flag = true;
        for (let i = 0; i < res.data.data.length; i++) {
          let flag = true;
          for (let k = 0; k < norepeat.length; k++) {
            if (norepeat[k].name === res.data.data[i].name) {
              flag = false;
              return;
            }
          }
          if (flag) {
            norepeat.push(res.data.data[i]);
          }

          // console.log(norepeat);
          this.searchList = norepeat;
        }
      });
    },

    searchTextChange(searchText) {
      //选中的时候发生的事件
      //发送请求,更新列表
      console.log(searchText);
      api.searchStuApi(null, searchText, null).then((res) => {
        this.currentPage = 1;
        this.SET_STU_DATA(res.data); //vuex中更新表格数据的方法
      });
    },

    onSearchButtonClick() {
      //点击搜索按钮
      console.log(this.searchValue);
      api.searchStuApi(null, this.searchValue, null).then((res) => {
        console.log(res);
        this.SET_STU_DATA(res.data);
      });
    },
  },
};
</script>
<style scoped>
.search {
  line-height: 60px;
  text-align: left;
  margin: 20px 0;
}
.select-class {
  line-height: 40px;
  text-align: left;
  margin-bottom: 20px;
}
.stu-table .btn {
  width: 65px;
  height: 25px;
  padding: 0;
}
.pagination {
  margin-top: 20px;
}
.exportExcel >>> i.icon-excel {
  color: green;
  font-size: 20px;
  margin-right: 3px;
}

.exportExcel >>> .el-button {
  padding: 4px 7px;
  float: right;
}
.exportExcel {
  margin-bottom: 10px;
  overflow: hidden;
}
</style>